package com.example.myvideoplayer.module;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import java.io.IOException;

public class VideoPlayer implements VideoPlayerModule {
    private static final String TAG = "video_player";
    private boolean seekBarIsChanging = false;
    private boolean isFinish = false;
    private int savePosition = 0;
    public final static int PLAY = 0;
    public final static int PAUSE = 1;
    public final static int SET_SEEKBAR = 3;
    public final static int FINISH = 4;
    public final static int SET_SURFACE_VIEW = 5;
    private Handler handler;
    private static VideoPlayer videoPlayer;
    private MediaPlayer mediaPlayer;
    private String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/VID_20190221_141418.mp4";

    private VideoPlayer() {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
        }
    }

    @Override
    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public static VideoPlayer getInstance() {
        if (videoPlayer == null) {
            videoPlayer = new VideoPlayer();
        }
        return videoPlayer;
    }

    @Override
    public void setGLSurface(Surface surface) {
        Log.i(TAG, "setsurface: ");
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
        }
        mediaPlayer.setSurface(surface);
    }

    public void play(String filePath) {
        Log.i(TAG, "play: ");
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
        }
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(filePath);
            Log.i(TAG, "play: " + filePath);
            mediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.prepareAsync();
            handler.sendEmptyMessage(PLAY);
            isFinish = false;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.i("3333", "onError: " + what);
                return false;
            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.i("3333", "onCompletion: ");
                isFinish = true;
                handler.sendEmptyMessage(PAUSE);
                handler.sendEmptyMessage(FINISH);
            }
        });
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
                mediaPlayer.seekTo(savePosition);
                savePosition = 0;
                int videoWidth = mediaPlayer.getVideoWidth();
                int videoHeight = mediaPlayer.getVideoHeight();
                Message message = Message.obtain(handler);
                message.what = SET_SURFACE_VIEW;
                message.arg1 = videoWidth;
                message.arg2 = videoHeight;
                handler.sendMessage(message);
                Log.i("3333", "onPrepared: " + videoWidth + "-----" + videoHeight);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (!isFinish) {
                            if (!seekBarIsChanging && mediaPlayer.isPlaying()) {
                                Message message = Message.obtain(handler);
                                message.what = SET_SEEKBAR;
                                message.arg1 = mediaPlayer.getCurrentPosition();
                                handler.sendMessage(message);
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }).start();
            }
        });
    }

    @Override
    public void playOrPause() {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                Log.i("video_player", "playOrPause: ");
                handler.sendEmptyMessage(PAUSE);
            } else {
                mediaPlayer.start();
                handler.sendEmptyMessage(PLAY);
            }
        }
    }

    @Override
    public void stop() {
        isFinish = true;
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
        videoPlayer = null;
    }
    public int getDuration() {
        if (mediaPlayer != null) {
            return mediaPlayer.getDuration();
        }
        return 0;
    }

    public void setSeekBarIsChanging(boolean seekBarIsChanging) {
        this.seekBarIsChanging = seekBarIsChanging;
    }
    public void setFinish(boolean isFinish){
        this.isFinish = isFinish;
    }

    public boolean isPlaying() {
        return mediaPlayer.isPlaying();
    }
    public void savePosition(){
        this.savePosition = mediaPlayer.getCurrentPosition();
    }
    public void seekTo(int position) {
        mediaPlayer.seekTo(position);
    }
}
